package com.liuyun.scheduledemo.controller;

import com.liuyun.scheduledemo.schedule.ScheduleInfo;
import com.liuyun.scheduledemo.schedule.ScheduleUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * Copyright: (C), 2021/7/21 15:55
 * <p>
 * <p>
 * Company Information & Data Security Solutions Co., Ltd.
 *
 * @author LiuYun liujw@idss-cn.com
 * @version 1.0
 */
@RestController
@RequestMapping("/schedule2")
public class ScheduleController {

    @Autowired
    private ScheduleUtils scheduleUtils;

    @RequestMapping("/addCronTask")
    public Object addCronTask(@RequestBody TaskVo taskVo) {
        ScheduleInfo scheduleInfo = new ScheduleInfo();
        BeanUtils.copyProperties(taskVo, scheduleInfo);
        scheduleUtils.addCronTriggerTask(scheduleInfo);
        return taskVo;
    }

    @RequestMapping("/addPeriodTask")
    public Object addPeriodTask(@RequestBody TaskVo taskVo) {
        ScheduleInfo scheduleInfo = new ScheduleInfo();
        BeanUtils.copyProperties(taskVo, scheduleInfo);
        scheduleUtils.addPeriodicTriggerTask(scheduleInfo);
        return taskVo;
    }

    @RequestMapping("/cancel")
    public Object cancel(@RequestBody TaskVo taskVo) {
        scheduleUtils.cancelTriggerTask(taskVo.getTaskId());
        return taskVo.getTaskId();
    }

    @RequestMapping("/modify")
    public Object modify(@RequestBody TaskVo taskVo) {
        ScheduleInfo scheduleInfo = new ScheduleInfo();
        BeanUtils.copyProperties(taskVo, scheduleInfo);
        scheduleUtils.modifyTriggerTask(scheduleInfo);
        return taskVo.getTaskId();
    }
}
